package uc.contancts;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import cn.onekit.CONFIG;
import cn.onekit.JSON;
import cn.onekit.android.ACTIVITY;
import co.chuangdian.oflauncher.OFActivity;
import co.chuangdian.oflauncher.R;

public class ContanctsActivity extends OFActivity {

	private uc.contancts.ContactsView contactsView;
	private ImageView iv_submit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_contancts);

		iv_submit = (ImageView) findViewById(R.id.submit);

		iv_submit.setOnClickListener(listener);

		contactsView = (uc.contancts.ContactsView) findViewById(R.id.ContactsView);

		contactsView.setData(ContanctsInfo.getContanctsIds(this));

	}

	OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			
			ArrayList<String> contactsPickIds = contactsView.getData();
			
			
			JSONArray contactsIds = JSON.fromList(contactsPickIds);

			JSONObject systemwidgets = JSON.parse(CONFIG.get("systemwidgets"));
			try {
				systemwidgets.put("contacts", contactsIds);
				CONFIG.set("systemwidgets", systemwidgets);
			} catch (JSONException e) {
				e.printStackTrace();
			}
			
			//preferences.edit().putString("contacts", contactsPickIds.toString()).commit();
			// 发送自定义广播
			Intent intent = new Intent();
			// 广播中的action也是自定义的
			intent.setAction("co.chuangdian.oflauncher.contacts");
			sendBroadcast(intent);
			
			finish();
			/*Toast.makeText(ContanctsActivity.this, "dianji", Toast.LENGTH_SHORT).show();*/
		}
	};
}
